﻿using ErrorOr;
using Gym.Application.Common.Interfaces;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Application.Gyms.Queries.GetGym
{
    public class GetGymQueryHandler : IRequestHandler<GetGymQuery, ErrorOr<Gym.Domain.GymAggregate.Gym>>
    {
        private readonly IGymsRepository _gymsRepository;
        private readonly ISubscriptionsRepository _subscriptionsRepository;

        public GetGymQueryHandler(IGymsRepository gymsRepository, ISubscriptionsRepository subscriptionsRepository)
        {
            _gymsRepository = gymsRepository;
            _subscriptionsRepository = subscriptionsRepository;
        }

        public async Task<ErrorOr<Domain.GymAggregate.Gym>> Handle(GetGymQuery request, CancellationToken cancellationToken)
        {
            if (await _subscriptionsRepository.ExistsAsync(request.SubscriptionId))
            {
                return Error.NotFound(description:"没有找到订阅信息");
            }

            var gym = await _gymsRepository.GetByIdAsync(request.GymId);
            if(gym is not Gym.Domain.GymAggregate.Gym)
            {
                return Error.NotFound(description:"没有找到场馆信息");
            }
            return gym;
        }
    }
}
